package com.simple.portal.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.portal.bo.IUserInfoBO;
import com.simple.portal.vo.UserInfoVO;
import com.simple.util.common.Constants;
import com.simple.util.common.ErrorUtil;
import com.simple.util.common.LocaleUtil;
import com.simple.util.common.RightsConstants;
import com.simple.util.exception.SimpleApplicationException;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.portal.GroupRightBean;
import com.simple.util.object.portal.SessionUserBean;

import freemarker.template.TemplateException;

@Controller

public class IndexController extends AbstractPortalBaseController{
	private final static String JSP_PATH = "_Index";
	
	
	@Autowired
	private IUserInfoBO userInfoBO;

	@RequestMapping("/index")
	public String index(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {

        return getJspPath("index");
    }
	
	@RequestMapping(value="/index/changegroup",method=RequestMethod.POST)  
	public String changegroup(@RequestParam(value="groupId",required=true) String groupId,
						final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		boolean flag = checkRights( request,response,RightsConstants.FUNC_INDEX);
		if(flag){
			final List<JFValidationBean> errorList = getErrorList();
			SessionUserBean sessionUserBean=getCurrentSessionUserBean(request);
			for(GroupRightBean groupRightBean:sessionUserBean.getGroupRightBeanList()){
				if(groupRightBean.getGroupInfoVO().getGroupId().equals(groupId)){

					if (Constants.STATE_STOP.equals(groupRightBean.getGroupInfoVO().getStatus())) {
						errorList.add(new JFValidationBean("message.login.user.group.status.not.setup"));
					}else{
						sessionUserBean.setGroupInfoVO(groupRightBean.getGroupInfoVO());
						sessionUserBean.setRightInfoVOListO(groupRightBean.getRightInfoVOListO());
						sessionUserBean.setRightInfoVOListA(groupRightBean.getRightInfoVOListA());
						sessionUserBean.setParentMenuVOList(groupRightBean.getParentMenuVOList());
						sessionUserBean.setChildMenuVOList(groupRightBean.getChildMenuVOList());
						request.getSession().setAttribute(Constants.CURRENT_SESSION_USERBEAN,sessionUserBean);
					}
					break;
				}
				
			}
			
		}

        return getJspPath("partner");
    }
	
	@RequestMapping("/index/init")  
    public String init(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		String returnUrlIndex="";

		SessionUserBean sessionUserBean=getCurrentSessionUserBean(request);
		String orgType=sessionUserBean.getOrganizationVO().getOrganizationType();
		if(Constants.ORGANIZATION_TYPE_SYSTEM.equals(orgType)){
			returnUrlIndex = system(request, response);
		}
		if(Constants.ORGANIZATION_TYPE_ENTERPRISE.equals(orgType)){
			returnUrlIndex = enterprise(request, response);
		}
		
		if(Constants.ORGANIZATION_TYPE_PARTNER.equals(orgType)){
			returnUrlIndex = partner(request, response);
		}

        return returnUrlIndex;
    }
	
	private String system(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
        
        return getJspPath("system");
    }
	
	private String enterprise(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		
        return getJspPath("enterprise");
    }
	
	private String partner(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		
		
        return getJspPath("partner");
    }
	
	@RequestMapping(value="/index/login")  
    public String login(@ModelAttribute("userInfoVO") UserInfoVO userInfoVO,
			    		final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException, ServletException  {
		final List<JFValidationBean> errorList = getErrorList();
			try {
				userInfoBO.checkLogin(userInfoVO.getPassword(),userInfoVO.getLoginId());
				
				request.getSession().setAttribute(Constants.CURRENT_SESSION_USERBEAN,userInfoBO.getSessionUserBeanByLoginId(userInfoVO.getLoginId()));
				
			} catch (SimpleApplicationException e) {
				errorList.add(ErrorUtil.generateJFValidationFromAppException(e));
			}

		return errorList.isEmpty() ? "forward:/index/init" : "forward:/index";
    }
	
	@RequestMapping("/logout")  
    public String logout(final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException, ServletException  {
		
		String currectLanguage=LocaleUtil.getLocaleFromRquest(request);
		
		request.getSession().invalidate();
        
        return "redirect:/index?locale="+currectLanguage;
    }

	@Override
    protected String getBaseJspPath() {
        return JSP_PATH;
    }
}

